package com.dyna.cp.elasticsearch.controller;

import com.dyna.cp.elasticsearch.domain.entity.Dyna;
import com.dyna.cp.elasticsearch.domain.param.DynaParam;
import com.dyna.cp.elasticsearch.service.DynaEsService;
import com.dyna.domain.resp.PageResult;
import com.dyna.domain.resp.R;
import com.dyna.resp.GroupResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zhoucaiwang
 */
@Api(value = "demo模块/cp/elasticsearch")
@RestController
@AllArgsConstructor
@RequestMapping("/es")
public class DynaEsController {

    private DynaEsService dynaEsService;

    @ApiOperation(value = "查询所有索引")
    @GetMapping("/queryAllIndex")
    public R<List<String>> queryAllIndex() {
        return R.data(dynaEsService.getIndices());
    }

    @ApiOperation(value = "添加索引")
    @GetMapping("/addIndex")
    public R<Boolean> addIndex(@RequestParam(value = "index",required = true) String index) {
        return R.data(dynaEsService.addIndex(index));
    }

    @ApiOperation(value = "删除索引")
    @GetMapping("/delIndex")
    public R<Boolean> delIndex(@RequestParam(value = "index",required = true) String index) {
        return R.data(dynaEsService.delIndex(index));
    }

    @ApiOperation(value = "索引是否存在")
    @GetMapping("/isExist")
    public R<Boolean> isExist(@RequestParam(value = "index",required = true) String index) {
        return R.data(dynaEsService.isExist(index));
    }

    @ApiOperation(value = "添加文档")
    @PostMapping("/addData")
    public R<String> addData(@RequestParam(value = "index",required = true) String index,
                             @RequestBody Map<String, Object> dyna) {
        return R.data(dynaEsService.addData(index,dyna));
    }

    @ApiOperation(value = "批量添加文档")
    @PostMapping(value = "/addBatchData")
    public R<Boolean> addBatchData(@RequestParam(value = "index",required = true) String index,
                                   @RequestBody List<Dyna> data) {
        return R.data(dynaEsService.addBatchData(index,data));
    }

    @ApiOperation(value = "滚动查询所有数据")
    @RequestMapping("/queryDataAll")
    public R<PageResult<?>> queryDataAll(@RequestParam(value = "index",required = true) String index) {
        return R.data(dynaEsService.queryDataAll(index));
    }
    @ApiOperation(value = "分页查询所有数据")
    @RequestMapping("/queryDataPage")
    public R<PageResult<?>> queryDataPage(@RequestParam(value = "index",required = true) String index,
                                      @RequestBody DynaParam dynaParam) {
        return R.data(dynaEsService.queryDataPage(index,dynaParam));
    }

    @ApiOperation(value = "根据id查询详细信息")
    @RequestMapping("/queryDataById")
    public R<Object> queryDataById(@RequestParam(value = "index",required = true) String index,
                                          @RequestParam(value = "id",required = true) String id) {
        return R.data(dynaEsService.queryDataById(index,id));
    }

    @ApiOperation(value = "修改文档数据")
    @RequestMapping("/updateData")
    public R<Boolean> updateData(@RequestParam(value = "index",required = true) String index,
                                 @RequestParam(value = "id",required = true) String id,
                                 @RequestBody Dyna dyna) {
        return R.data(dynaEsService.updateData(index,id,dyna));
    }

    @ApiOperation(value = "删除文档数据")
    @RequestMapping("/deleteData")
    public R<Boolean> deleteData(@RequestParam(value = "index",required = true) String index,
                                 @RequestParam(value = "id",required = true) String id) {
        return R.data(dynaEsService.deleteData(index,id));
    }

    @ApiOperation(value = "分组统计")
    @PostMapping("/group")
    public R<List<Map<String, Object>>> group(@RequestParam(value = "index",required = true) String index,
                                              @RequestParam(value = "field",required = true) String field,
                                              @RequestParam(value = "type",required = true) String type) {
        List<Map<String, Object>> data = dynaEsService.group(index, field, type);
        return R.data(data);
    }

    @ApiOperation(value = "查询最大值和最小值")
    @GetMapping("/stats")
    public R<Double> getScoreStats(@RequestParam(value = "index", required = true) String index,
                                    @RequestParam(value = "field", required = true) String field,
                                    @RequestParam(value = "type", required = true) Boolean type) {
        Double score = dynaEsService.getScoreStats(index,field,type);
        return R.data(score);
    }

    @ApiOperation(value = "统计分组中数量、最大值和最小值")
    @GetMapping("/groupStats")
    public R<List<GroupResp>> getGroupStats(@RequestParam(value = "index", required = true) String index,
                                            @RequestParam(value = "groupField", required = true) String groupField,
                                            @RequestParam(value = "staticField", required = true) String staticField,
                                            @RequestParam(value = "type", required = true) Boolean type,
                                            @RequestParam(value = "sort", required = true) String sort) {
        return R.data(dynaEsService.getGroupStats(index,groupField,staticField,type,sort));
    }

    @ApiOperation(value = "查询网关请求日志")
    @GetMapping("/getGatewayLogByTraceId")
    public R<Map<String, Object>> getGatewayLogByTraceId(@RequestParam(value = "traceId",required = true) String traceId) {
        return R.data(dynaEsService.getGatewayLogByTraceId(traceId));
    }

}
